{%- comment %}
  Look up the nav bar using the version from the URL. We need to assign
  temporary variables because liquid does not support nested []'s.

  Remember to update pagination.html if you update this.
{%- endcomment %}

{%- comment %}Find the right collection from the navigation data{% endcomment %}
{%- assign nav = site.data.nav["main"] %}
{%- for item in nav %}
  {% if item.url == page.dir or item.url == page.url %}
    {% assign this = item %}
    {% break %}
  {% endif %}
{% endfor %}
{%- if this == nil %}
  {%- comment %}Didn't find item in main menu, revert to old behavior for backward compatibility, without expanding the menu if it's the main (displayed on blog posts among other pages).{% endcomment %}
  {%- assign url_parts = page.url | split: '/' %}
  {%- assign part_1 = url_parts[1] %}
  {%- assign part_2 = url_parts[2] %}
  {%- assign key = site.data.nav.config.url_to_key[part_1]
      | default: site.data.nav.config.url_to_key[part_2] %}
  {%- if key == nil %}
    {%- assign expand = false %}
    {%- assign key = "main" %}
  {%- else %}
    {%- assign expand = true %}
  {%- endif %}
  {%- assign nav = site.data.nav[key] %}
{% else %}
  {%- assign expand = false %}
{% endif %}
<nav class="site-nav">
  {%- include nav-level.html collection=nav expand=expand %}
</nav>
